package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategyCover;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.mapper.StrategyCoverMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper mapper;
    @Autowired
    private StrategyCoverMapper coverMapper;


    @Override
    public PageInfo<StrategyComment> selectList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyComment> list = mapper.selectForList(qo);
        return new PageInfo<>(list);
    }


    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        Date date = new Date();
        if (strategyComment.getId() == null) {
            strategyComment.setCreateTime(date);
            mapper.insert(strategyComment);
            List<StrategyCover> images = strategyComment.getImages();
            for (StrategyCover image : images) {
                image.setCommentId(strategyComment.getId());
                coverMapper.insert(image);
            }


        } else {
            mapper.updateByPrimaryKey(strategyComment);
        }
    }

    @Override
    public StrategyComment selectById(Long id) {
        return mapper.selectByPrimaryKey(id);

    }
}
